package com.ekingstar.lss.demo.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 *	公司：金仕达多媒体
 *	作者：童弘成
 *	时间：2011-5-3 下午03:53:21
 *	描述：
 */
public class TreeDataAction {
	public String xml(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		try {
			String id=request.getParameter("id");
			if(id==null||"".equals(id)){
				id="0";
			}
			Document document=DocumentHelper.createDocument();
			Element root=DocumentHelper.createElement("tree");
			Attribute rootid=DocumentHelper.createAttribute(root, "id", id);
			root.add(rootid);
			
			for (int i = 0; i < 5; i++) {
				Element item=DocumentHelper.createElement("item");
				item.add(DocumentHelper.createAttribute(item, "id", id+"_"+i));
				item.add(DocumentHelper.createAttribute(item, "text", id+"_"+i));
				item.add(DocumentHelper.createAttribute(item, "checked", "yes"));
				item.add(DocumentHelper.createAttribute(item, "child", "1"));
				Element userdata=DocumentHelper.createElement("userdata");
				userdata.add(DocumentHelper.createAttribute(userdata, "name", "url"));
				userdata.add(DocumentHelper.createText("http://www.126.com"));
				item.add(userdata);
				root.add(item);
			}
			document.add(root);
			PrintWriter pw=response.getWriter();
			OutputFormat format=OutputFormat.createCompactFormat();
			format.setEncoding("utf-8");
			XMLWriter writer=new XMLWriter(pw,format);
			writer.write(document);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
